package com.gewu.pm.ui.activity.home;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import androidx.viewpager.widget.ViewPager;

import com.androidkun.xtablayout.XTabLayout;
import com.gewu.pm.R;
import com.gewu.pm.aop.SingleClick;
import com.gewu.pm.app.AppActivity;
import com.gewu.pm.app.AppFragment;
import com.gewu.pm.other.IntentKey;
import com.gewu.pm.ui.activity.filter.FilterMultipleActivity;
import com.gewu.pm.ui.fragment.home.PaymentStatementFragment;
import com.gewu.pm.ui.fragment.home.SalesStatementFragment;
import com.gewu.pm.ui.popup.SelectTimePopup;
import com.hjq.base.BasePopupWindow;
import com.hjq.base.FragmentPagerAdapter;

import java.util.ArrayList;
import java.util.List;

/**
 * 客户对账单Activity
 */
public final class CustomerStatementActivity extends AppActivity {

    private TextView mCustomerNameView;
    private TextView mDetailView;
    private TextView mPendingAmountView;
    private TextView mSettledAmountView;
    private TextView mDateFilterView;
    private XTabLayout mTabLayout;
    private ViewPager mViewPager;

    private FragmentPagerAdapter<AppFragment<?>> mPagerAdapter;
    private SalesStatementFragment mSalesFragment;
    private PaymentStatementFragment mPaymentFragment;

    public static void start(Context context, String id) {
        Intent intent = new Intent(context, CustomerStatementActivity.class);
        intent.putExtra(IntentKey.ID, id);
        context.startActivity(intent);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.customer_statement_activity;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        mCustomerNameView = findViewById(R.id.tv_customer_name);
        mDetailView = findViewById(R.id.tv_detail);
        mPendingAmountView = findViewById(R.id.tv_pending_amount);
        mSettledAmountView = findViewById(R.id.tv_settled_amount);
        mDateFilterView = findViewById(R.id.tv_date_filter);
        mTabLayout = findViewById(R.id.tab_layout);
        mViewPager = findViewById(R.id.vp_content);

        // 设置点击事件
        setOnClickListener(mDetailView, mDateFilterView);

        // 初始化 Fragment
        mSalesFragment = SalesStatementFragment.newInstance();
        mPaymentFragment = PaymentStatementFragment.newInstance();

        // 创建 FragmentPagerAdapter
        mPagerAdapter = new FragmentPagerAdapter<>(this);
        mPagerAdapter.addFragment(mSalesFragment, "销售单据");
        mPagerAdapter.addFragment(mPaymentFragment, "收款单据");
        
        // 设置 ViewPager
        mViewPager.setAdapter(mPagerAdapter);
        mTabLayout.setupWithViewPager(mViewPager);
    }

    @Override
    protected void initData() {
        // 初始化数据
    }

    @SingleClick
    @Override
    public void onClick(View view) {
        super.onClick(view);
        int viewId = view.getId();
        if (viewId == R.id.tv_detail) {
            // 点击详情
            toast("详情功能");
        } else if (viewId == R.id.tv_date_filter) {
            // 点击日期筛选
            showDateFilterPopup();
        }
    }

    @Override
    public void onRightClick(View view) {
        super.onRightClick(view);
        // 点击筛选按钮
        FilterMultipleActivity.start(getActivity());
    }

    /**
     * 显示日期筛选弹窗
     * 
     * 功能说明：
     * 1. 创建日期选项列表（默认7天、今日、7天、30天）
     * 2. 显示下拉选择弹窗
     * 3. 选中后更新显示的日期文本
     * 4. 通知 Fragment 刷新数据
     */
    private void showDateFilterPopup() {
        // 创建日期选项列表
        List<String> options = new ArrayList<>();
        options.add("默认7天");
        options.add("今日");
        options.add("7天");
        options.add("30天");

        // 获取当前显示的日期文本（用于设置选中状态）
        String currentText = mDateFilterView.getText().toString();

        // 创建并显示日期选择弹窗
        new SelectTimePopup.Builder(this)
                .setList(options)
                .setSelectedItem(currentText)
                .setListener(new SelectTimePopup.OnListener() {
                    @Override
                    public void onSelected(BasePopupWindow popupWindow, String category) {
                        // 更新显示的日期文本
                        mDateFilterView.setText(category);
                        
                        // 通知当前显示的 Fragment 刷新数据
                        int currentItem = mViewPager.getCurrentItem();
                        if (currentItem == 0) {
                            // 销售单据 Fragment
                            mSalesFragment.setDateFilter(category);
                        } else {
                            // 收款单据 Fragment
                            mPaymentFragment.setDateFilter(category);
                        }
                    }
                })
                .showAsDropDown(mDateFilterView);
    }
}
